/*
 * request_panel.c
 * This file is part of gURL
 *
 * Copyright (C) 2013, 2014 - Aleksey Konovalov
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Library General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "request_panel.h"

#include "defines.h"
#include <glib/gi18n.h>
#include "highlighter/highlighter.h"
#include <stdbool.h>
#include "utils.h"

struct _RequestPanel {
    GtkNotebook parent;

    GtkWidget* headers;
    GtkWidget* body;
    GtkWidget* body_encoding;
};

struct _RequestPanelClass {
    GtkNotebookClass parent_class;
};

G_DEFINE_TYPE(RequestPanel, request_panel, GTK_TYPE_NOTEBOOK)

static void request_panel_class_init(RequestPanelClass* UNUSED(klass)) {}

static GtkWidget* create_text_view(GtkWidget** tw) {
    *tw = gtk_text_view_new();
    gtk_text_view_set_editable(GTK_TEXT_VIEW(*tw), FALSE);
    gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(*tw), FALSE);
    gtk_widget_show(*tw);
    GtkWidget* sw = add_to_scrolled_window(*tw);
    return sw;
}

static void request_panel_init(RequestPanel* self) {
    GtkWidget* sw = create_text_view(&self->headers);
    g_object_set(G_OBJECT(sw), "margin", 7, NULL);
    gtk_notebook_append_page(GTK_NOTEBOOK(self), sw,
                             gtk_label_new(_("Headers")));

    GtkWidget* body_panel = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
    gtk_widget_show(body_panel);
    g_object_set(G_OBJECT(body_panel), "margin", 7, NULL);
    gtk_notebook_append_page(GTK_NOTEBOOK(self), body_panel,
                             gtk_label_new(_("Body")));

    sw = create_text_view(&self->body);
    gtk_box_pack_start(GTK_BOX(body_panel), sw, TRUE, TRUE, 0);

    self->body_encoding = gtk_label_new("");
    gtk_misc_set_alignment(GTK_MISC(self->body_encoding), 1, 0.5);
    gtk_box_pack_end(GTK_BOX(body_panel), self->body_encoding,
                       FALSE, FALSE, 0);
}

void request_panel_set_session(RequestPanel* self, Session* session) {
    GtkTextBuffer* hdr_tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(self->headers));
    GtkTextBuffer* body_tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(self->body));
    if(session) {
        const GString* headers = session_get_request_headers(session);
        gtk_text_buffer_set_text(hdr_tb, headers->str, headers->len);
        highlight_http_headers(hdr_tb, headers->str, headers->len);

        Encoding body_enc = session_get_request_body_encoding(session);

        const GString* body = session_get_request_body(session);
        GString* body_decoded = decode_string(body->str, body->len, body_enc);
        gtk_text_buffer_set_text(body_tb, body_decoded->str, body_decoded->len);
        g_string_free(body_decoded, TRUE);

        char buf[256];
        g_snprintf(buf, sizeof(buf), "%s: %s", _("Encoding"),
                   get_encoding_name(body_enc));
        gtk_label_set_text(GTK_LABEL(self->body_encoding), buf);
        gtk_widget_show(self->body_encoding);
    } else {
        gtk_text_buffer_set_text(hdr_tb, "", 0);
        gtk_text_buffer_set_text(body_tb, "", 0);
        gtk_widget_hide(self->body_encoding);
    }
}

GtkWidget* request_panel_new() {
    return g_object_new(REQUEST_PANEL_TYPE, NULL);
}
